# coding: utf8

#########################################################################
## This is a samples controller
## - index is the default action of any application
## - user is required for authentication and authorization
## - download is for downloading files uploaded in the db (does streaming)
## - call exposes all registered services (none by default)
#########################################################################  


def index():
    return dict()


def change():
    templates = db().select(db.templates.ALL)
    swfs = []
    swf = ''
    for template in templates:
        swfs.append(template['swfpath']+'/'+template['swfname'])
    if swfs == []:
        response.flash = "templates is empty, need add something"
        redirect(URL(r=request, c='template', f='add'))
    return dict(swf = swf, swfs = swfs)


def images():
    images = db().select(db.images.ALL)
    return dict(images = images)


@auth.requires_membership('power')
def data(): return dict(form=crud())


def user():
    """
    exposes:
    http://..../[app]/default/user/login 
    http://..../[app]/default/user/logout
    http://..../[app]/default/user/register
    http://..../[app]/default/user/profile
    http://..../[app]/default/user/retrieve_password
    http://..../[app]/default/user/change_password
    use @auth.requires_login()
        @auth.requires_membership('group name')
        @auth.requires_permission('read','table name',record_id)
    to decorate functions that need access control
    """
    return dict(form=auth())


def download():
    """
    allows downloading of uploaded files
    http://..../[app]/default/download/[filename]
    """
    return response.download(request,db)


def call():
    """
    exposes services. for example:
    http://..../[app]/default/call/jsonrpc
    decorate with @services.jsonrpc the functions to expose
    supports xml, json, xmlrpc, jsonrpc, amfrpc, rss, csv
    """
    return service()

@set_user
def temp():
    pass